package com.example.graduation.controller;

import com.example.graduation.entity.JobSeeker;
import com.example.graduation.service.JobSeekerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/job-seekers")
public class JobSeekerController {
    @Autowired
    private JobSeekerService jobSeekerService;

    @GetMapping
    public ResponseEntity<List<JobSeeker>> getAllJobSeekers() {
        List<JobSeeker> jobSeekers = jobSeekerService.findAll();
        return ResponseEntity.ok(jobSeekers);
    }

    @GetMapping("/{id}")
    public ResponseEntity<JobSeeker> getJobSeekerById(@PathVariable Integer id) {
        JobSeeker jobSeeker = jobSeekerService.findById(id);
        if (jobSeeker != null) {
            return ResponseEntity.ok(jobSeeker);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @GetMapping("/email/{email}")
    public ResponseEntity<JobSeeker> getJobSeekerByEmail(@PathVariable String email) {
        JobSeeker jobSeeker = jobSeekerService.findByEmail(email);
        if (jobSeeker != null) {
            return ResponseEntity.ok(jobSeeker);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @PostMapping
    public ResponseEntity<JobSeeker> createJobSeeker(@RequestBody JobSeeker jobSeeker) {
        int result = jobSeekerService.insert(jobSeeker);
        if (result > 0) {
            return ResponseEntity.ok(jobSeeker);
        } else {
            return ResponseEntity.badRequest().build();
        }
    }

    @PutMapping("/{id}")
    public ResponseEntity<JobSeeker> updateJobSeeker(@PathVariable Integer id, @RequestBody JobSeeker jobSeeker) {
        jobSeeker.setId(id);
        int result = jobSeekerService.update(jobSeeker);
        if (result > 0) {
            return ResponseEntity.ok(jobSeeker);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteJobSeeker(@PathVariable Integer id) {
        int result = jobSeekerService.deleteById(id);
        if (result > 0) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }
} 